package cn.idongjia.video;

import cn.idongjia.log.Log;
import cn.idongjia.log.LogFactory;
import cn.idongjia.message.consumer.IMessageHandler;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.media.MediaConfiguration;
import com.alibaba.media.Result;
import com.alibaba.media.client.impl.DefaultMediaClient;
import com.alibaba.media.manage.GlobalFileResource;
import com.alibaba.media.manage.ManageClient;
import com.alibaba.media.manage.MediaEncodeRequest;

import java.util.Map;

public class VideoEncode implements IMessageHandler<String, String> {

    private static final Log log = LogFactory.getLog(VideoEncode.class);
    private static final ManageClient client;
    private static String ak="23327189";
    private static String sk="6a7c16c39cb26063b8743f3f64126884";
    private static String namespace="dongjiaapp";
    private static String notifyUrl="http://app-test.kaipao.cc/v1/video/encode/notify/";
    private static String androidEncodeTemplate="video-generic-AVC-720p-16_9";
    private static String iosEncodeTemplate="video-HLS-640K";
    private static Map<String,String> config=VedioConfigLoader.getInstance().loadConfig();
    static {
        MediaConfiguration configuration = new MediaConfiguration();
        configuration.setAk(config.get(Constant.AK));
        configuration.setSk(config.get(Constant.SK));
        configuration.setNamespace(config.get(Constant.NAMESPACE));
        client = new DefaultMediaClient(configuration);
    }


    @Override
    public void handle(String key, String message) {
        if (StringUtils.isNotEmpty(message) && StringUtils.isNotEmpty(key)) {
            log.info("Key:{%s}======================Value:{%s}",key,message);
            long pid = Long.valueOf(key);
            String filePath = Utils.getFileDir(message);
            String fileName = Utils.getFileName(message);
            encodeAndroid(pid, filePath, fileName,config.get(Constant.NAMESPACE),config.get(Constant.ANDROIDENCODETEMPLATE),config.get(Constant.NOTIFYURL));
            encodeIos(pid, filePath, fileName,config.get(Constant.NAMESPACE),config.get(Constant.IOSENCODETEMPLATE),config.get(Constant.NOTIFYURL));
        }
    }

    private void encodeAndroid(long pid, String filepath, String filename,String namespace,String template,String notifyUrl) {
        MediaEncodeRequest request = new MediaEncodeRequest();
        request.setInput(new GlobalFileResource(namespace, filepath, filename));
        String videoName = Utils.getRandomName(".mp4");
        request.setOutput(new GlobalFileResource(namespace, "/mp4", videoName));
//        request.setWatermark(new GlobalFileResource(namespace, "/", "视频角标.png"));
        request.setNotifyUrl(notifyUrl + pid);
        request.setEncodeTemplate(template);
//        request.setWatermarkTemplate("symb");
        request.setUsePreset(false);
        request.setForce(true);
//        request.setSeek("0");//视频截取开始位置
//        request.setDuration("10");//视频截取长度
        Result<String> r = client.mediaEncode(request);
        log.info("=====================================" + r);
        if (r.isSuccess()) {
            log.info(videoName + "视频转换格式任务提交状态:{%s}", r.getData());
        }
    }

    private void encodeIos(long pid, String filepath, String filename,String namespace,String template,String notifyUrl) {
        MediaEncodeRequest request = new MediaEncodeRequest();
        request.setInput(new GlobalFileResource(namespace, filepath, filename));
        String videoName = Utils.getRandomName("");
        request.setOutput(new GlobalFileResource(namespace, "/hls/" + videoName, videoName));
//        request.setWatermark(new GlobalFileResource(namespace, "/", "视频角标.png"));
        request.setNotifyUrl(notifyUrl+pid);
        request.setEncodeTemplate(template);
//        request.setWatermarkTemplate("symb");
        request.setUsePreset(true);
        request.setForce(true);
//        request.setSeek("0");//视频截取开始位置
//        request.setDuration("10");//视频截取长度
        Result r = client.mediaEncode(request);
        log.info("=====================================" + r);
        if (r.isSuccess()) {
            log.info(videoName + "视频转换格式任务提交状态:{%s}", r.getData());
        }
    }


}
